package com.umanji;

import java.util.Collection;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.umanji.security.domain.Users;
import com.umanji.security.service.UserService;
import com.umanji.user.UserController;
import com.umanji.user.domain.Address;
import com.umanji.user.domain.Members;
import com.umanji.user.service.MemberService;

/**
 * Handles requests for the application welcome page.
 */
@Controller
@RequestMapping("/main")
public class MainController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(UserController.class);

    @Autowired
    private MemberService userService;
    
    @Autowired
    private UserService securityUserService;
    
    private Members member;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelMap versionMain(ModelMap model) {
		logger.info("version 1.1 main!");
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Users umanji_member = new Users();
	    if (principal instanceof UserDetails) {

	    	logger.info(":::::::::::::::::::::::::::::::::::::::: found user ::::::::::::::::::");
	    	umanji_member = securityUserService.findByUsername(((UserDetails) principal).getUsername());
	    }
	    else{
	    	logger.info("::::::::::::::::::::::::::::::::::::::::not found user ::::::::::::::::::");
	    }
	    
        model.addObject("user", umanji_member.getMember());
    	// userService.remove(user);
        return model;
	}

	@RequestMapping(value = "/usertest", method = RequestMethod.GET)
	public void userTest(ModelMap model) {
		logger.info("Welcome!");
		
		/*
		Address address = new Address("Evergreen Terrace", "99a", "Springfield", "57657", "Ohio", "USA");
    	member = new Members( new Users("testid", "testpassword", true));
    	member.setFirstName("firstName");
    	member.setMiddleName("middleName");
    	member.setLastName("lastName");
    	member.setEmailAddress("someone@gmail.com");
    	member.setPhoneNumber("1234567891");
    	member.setAddress(address);
		
    	userService.persist(member);
    	model.addAttribute("user", member);
    	*/
    	// userService.remove(user);
	}
}
